#include <stdio.h>
#include <SDL.h>

SDL_Surface* screen;

void pool_event();
void wait_event();
/*
 * Return the pixel value at (x, y)
 * NOTE: The surface must be locked before calling this!
 */
Uint32 getpixel(SDL_Surface *surface, int x, int y)
{
    int bpp = surface->format->BytesPerPixel;
    /* Here p is the address to the pixel we want to retrieve */
    Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;

    switch(bpp) {
    case 1:
        return *p;

    case 2:
        return *(Uint16 *)p;

    case 3:
        if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
            return p[0] << 16 | p[1] << 8 | p[2];
        else
            return p[0] | p[1] << 8 | p[2] << 16;

    case 4:
        return *(Uint32 *)p;

    default:
        return 0;       /* shouldn't happen, but avoids warnings */
    }
}

void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
{
    int bpp = surface->format->BytesPerPixel;
    /* Here p is the address to the pixel we want to set */
    Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;

    switch(bpp) {
    case 1:
        *p = pixel;
        break;

    case 2:
        *(Uint16 *)p = pixel;
        break;

    case 3:
        if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
            p[0] = (pixel >> 16) & 0xff;
            p[1] = (pixel >> 8) & 0xff;
            p[2] = pixel & 0xff;
        } else {
            p[0] = pixel & 0xff;
            p[1] = (pixel >> 8) & 0xff;
            p[2] = (pixel >> 16) & 0xff;
        }
        break;

    case 4:
        *(Uint32 *)p = pixel;
        break;
    }
}

void puttest(SDL_Surface* screen){
	int x, y;
	Uint32 yellow;

    /* Map the color yellow to this display (R=0xff, G=0xFF, B=0x00)
       Note:  If the display is palettized, you must set the palette first.
    */
    yellow = SDL_MapRGB(screen->format, 0xff, 0xff, 0x00);

    x = screen->w / 2;
    y = screen->h / 2;

    /* Lock the screen for direct access to the pixels */
    if ( SDL_MUSTLOCK(screen) ) {
        if ( SDL_LockSurface(screen) < 0 ) {
            fprintf(stderr, "Can't lock screen: %s\n", SDL_GetError());
            return;
        }
    }

    putpixel(screen, x, y, yellow);

    if ( SDL_MUSTLOCK(screen) ) {
        SDL_UnlockSurface(screen);
    }
    /* Update just the part of the display that we've changed */
    SDL_UpdateRect(screen, x, y, 1, 1);

    return;
}

void display_bmp(char *file_name)
{
    SDL_Surface *image;

    /* Load the BMP file into a surface */
    image = SDL_LoadBMP(file_name);
    if (image == NULL) {
        fprintf(stderr, "Couldn't load %s: %s\n", file_name, SDL_GetError());
        return;
    }

    /*
     * Palettized screen modes will have a default palette (a standard
     * 8*8*4 colour cube), but if the image is palettized as well we can
     * use that palette for a nicer colour matching
     */
    if (image->format->palette && screen->format->palette) {
    SDL_SetColors(screen, image->format->palette->colors, 0,
                  image->format->palette->ncolors);
    }

    /* Blit onto the screen surface */
    if(SDL_BlitSurface(image, NULL, screen, NULL) < 0)
        fprintf(stderr, "BlitSurface error: %s\n", SDL_GetError());

    SDL_UpdateRect(screen, 0, 0, image->w, image->h);

    /* Free the allocated BMP surface */
    SDL_FreeSurface(image);
}



int main(int argc,char** argv){
	printf("note this\n");
	if( SDL_Init( SDL_INIT_VIDEO|SDL_INIT_AUDIO)==-1){
		printf("could init %s\n",SDL_GetError());
		exit(-1);
	}
	
	printf("sdl inited\n");
	
	atexit(SDL_Quit);
	
	
	screen=SDL_SetVideoMode(800,600,16,SDL_SWSURFACE);
	if(screen==NULL){
		fprintf(stderr,"could switch to mode %s\n",SDL_GetError());
		exit(1);
	}
//	display_bmp("./beiying.bmp");
	puttest(screen);
	
	wait_event();
	//pool_event();

	exit(0);
}

void wait_event(){
	SDL_Event event;
	/* Ignore key events */
	//SDL_EventState(SDL_KEYDOWN, SDL_IGNORE);
	//SDL_EventState(SDL_KEYUP, SDL_IGNORE);

	    /* Filter quit and mouse motion events */
	    //SDL_SetEventFilter(FilterEvents);
	SDL_EnableUNICODE( 1 );
	/* Loop waiting for ESC+Mouse_Button */
	char c;
	    while ( SDL_WaitEvent(&event) >= 0 ) {
			//printf("%d\n",event.type);
	        switch (event.type) {
				case SDL_KEYDOWN:
                case SDL_KEYUP:
					c=event.key.keysym.sym;
					printf("%c %d\n",c,c);
					if(c=='q'){
						exit(0);
					}else if(c=='f'){
						SDL_WM_ToggleFullScreen(screen);
					}
					//PrintKeyInfo( &event.key );
                    break;

	            case SDL_ACTIVEEVENT: {
	                if ( event.active.state & SDL_APPACTIVE ) {
	                    if ( event.active.gain ) {
	                        printf("App activated\n");
	                    } else {
	                        printf("App iconified\n");
	                    }
	                }
	            }
	            break;

	            case SDL_MOUSEBUTTONDOWN: {
	                Uint8 *keys;

	                keys = SDL_GetKeyState(NULL);
	                if ( keys[SDLK_ESCAPE] == SDL_PRESSED ) {
	                    printf("Bye bye...\n");
	                    exit(0);
	                }
	                printf("Mouse button pressed\n");
	            }
	            break;

	            case SDL_QUIT: {
	                printf("Quit requested, quitting.\n");
	                exit(0);
	            }
	            break;
	        }
	    }
}

void pool_event(){
	SDL_Event event;
	int quit=0;
	
	/* Enable Unicode translation */
    SDL_EnableUNICODE( 1 );
	
	/* Loop until an SDL_QUIT event is found */
    while( !quit ){

        /* Poll for events */
        while( SDL_PollEvent( &event ) ){

            switch( event.type ){
                /* Keyboard event */
                /* Pass the event data onto PrintKeyInfo() */
                case SDL_KEYDOWN:
                case SDL_KEYUP:
					if(event.key.keysym.sym=='q'){
						quit=1;
						break;
					}
                    PrintKeyInfo( &event.key );
                    break;

                /* SDL_QUIT event (window close) */
                case SDL_QUIT:
                    quit = 1;
                    break;

                default:
                    break;
            }

        }

    }
}

/* Print all information about a key event */
void PrintKeyInfo( SDL_KeyboardEvent *key ){
  /* Is it a release or a press? */
  if( key->type == SDL_KEYUP )
      printf( "Release:- " );
  else
      printf( "Press:- " );

  /* Print the hardware scancode first */
  printf( "Scancode: 0x%02X", key->keysym.scancode );
  /* Print the name of the key */
  printf( ", Name: %s", SDL_GetKeyName( key->keysym.sym ) );
  /* We want to print the unicode info, but we need to make */
  /* sure its a press event first (remember, release events */
  /* don't have unicode info                                */
  if( key->type == SDL_KEYDOWN ){
      /* If the Unicode value is less than 0x80 then the    */
      /* unicode value can be used to get a printable       */
      /* representation of the key, using (char)unicode.    */
      printf(", Unicode: " );
      if( key->keysym.unicode < 0x80 && key->keysym.unicode > 0 ){
          printf( "%c (0x%04X)", (char)key->keysym.unicode,
                  key->keysym.unicode );
      }
      else{
          printf( "? (0x%04X)", key->keysym.unicode );
      }
  }
  printf( "\n" );
  /* Print modifier info */
  PrintModifiers( key->keysym.mod );
}

    /* Print modifier info */
void PrintModifiers( SDLMod mod ){
   printf( "Modifers: " );

   /* If there are none then say so and return */
   if( mod == KMOD_NONE ){
       printf( "None\n" );
       return;
   }

   /* Check for the presence of each SDLMod value */
   /* This looks messy, but there really isn't    */
   /* a clearer way.                              */
   if( mod & KMOD_NUM ) printf( "NUMLOCK " );
   if( mod & KMOD_CAPS ) printf( "CAPSLOCK " );
   if( mod & KMOD_LCTRL ) printf( "LCTRL " );
   if( mod & KMOD_RCTRL ) printf( "RCTRL " );
   if( mod & KMOD_RSHIFT ) printf( "RSHIFT " );
   if( mod & KMOD_LSHIFT ) printf( "LSHIFT " );
   if( mod & KMOD_RALT ) printf( "RALT " );
   if( mod & KMOD_LALT ) printf( "LALT " );
   if( mod & KMOD_CTRL ) printf( "CTRL " );
   if( mod & KMOD_SHIFT ) printf( "SHIFT " );
   if( mod & KMOD_ALT ) printf( "ALT " );
   printf( "\n" );
}

